package com.example.trans.controller;

import com.example.trans.service.UserService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RequestMapping("/trans")
@RestController
public class TransController {
    @Autowired
    private UserService userService;

    @Transactional
    @RequestMapping("register")
    public Boolean register(String username, String password) {
        Integer result = userService.insert(username, password);
        System.out.println("插入用户表, result: " + result);

        try {
            int a = 10/0;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 重新把异常跑出去, 事务回滚
     * @param username
     * @param password
     * @return
     */
    @Transactional
    @RequestMapping("r2")
    public Boolean r2(String username, String password) {
        Integer result = userService.insert(username, password);
        System.out.println("插入用户表, result: " + result);

        try {
            int a = 10/0;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 手动把事务进行回滚
     * @param username
     * @param password
     * @return
     */
    @Transactional
    @RequestMapping("r3")
    public Boolean r3(String username, String password) {
        Integer result = userService.insert(username, password);
        System.out.println("插入用户表, result: " + result);

        try {
            int a = 10/0;
        }catch (Exception e) {
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return true;
    }

    /**
     * 事务提交
     * @param username
     * @param password
     * @return
     * @throws IOException
     */
    @Transactional
    @RequestMapping("r4")
    public Boolean r4(String username, String password) throws IOException {
        Integer result = userService.insert(username, password);
        System.out.println("插入用户表, result: " + result);

        if(true) {
            throw new IOException();
        }
        return true;
    }

    /**
     * 事务回滚
     * @param username
     * @param password
     * @return
     */
    @SneakyThrows
    @Transactional
    @RequestMapping("r5")
    public Boolean r5(String username, String password){
        Integer result = userService.insert(username, password);
        System.out.println("插入用户表, result: " + result);

        if(true) {
            throw new IOException();
        }
        return true;
    }

    /**
     * 事务回滚
     * @param username
     * @param password
     * @return
     */
    @Transactional
    @RequestMapping("r6")
    public Boolean r6(String username, String password) throws Exception {
        Integer result = userService.insert(username, password);
        System.out.println("插入用户表, result: " + result);

        if(true) {
            throw new RuntimeException();
        }
        return true;
    }

    /**
     * 事务回滚
     * @param username
     * @param password
     * @return
     * @throws IOException
     */
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("r7")
    public Boolean r7(String username, String password) throws IOException {
        Integer result = userService.insert(username, password);
        System.out.println("插入用户表, result: " + result);

        if(true) {
            throw new IOException();
        }
        return true;
    }

}
